package com.demo0215.controller;

import com.demo0215.entity.User;
import com.demo0215.entity.UserVo;
import com.demo0215.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RefreshScope
@RequestMapping("/user")
public class UserController {

   @Autowired
   private UserRepository userRepository;

    @GetMapping("/findAll/{index}/{limit}")
    public UserVo findAll(@PathVariable("index") int index, @PathVariable("limit") int limit){
        List<User> user = userRepository.findAll(index, limit);
        return new UserVo(200,"OK", userRepository.count(), user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        userRepository.deleteById(id);
    }

    @PostMapping("/save")
    public void save(@RequestBody  User user){
        user.setRegisterdate(new Date());
        userRepository.save(user);
    }

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") long id){
        return userRepository.findById(id);
    }

    @PutMapping("/update")
    public void update(@RequestBody User user){
        userRepository.update(user);
    }

    @GetMapping("/count")
    public int count(){
        return userRepository.count();
    }

    @PostMapping("/userlogin")
    User loginByIdPass(@RequestBody User user){
        User user1 = userRepository.loginByIdPassword(user);
        System.out.println(user1);
        return user1;
    }
}
